home *** CD-ROM | disk | FTP | other *** search
- /* util.c - Utilities for GNUPLOTIO */
-
- #include <stdio.h> /* FILENAME_MAX, NULL */
- #include <stdlib.h> /* getenv */
- #include <string.h> /* strcpy/cat */
- #include <sys/stat.h> /* stat */
- #include <sys/time.h> /* select */
- int bzero(void *, int); /* this isn't in a header file !? */
- #include "util.h"
-
- /*
- || check_fd_for_reading
- ||
- || Find out whether a read(fd,...) would block.
- ||
- */
- int check_fd_for_reading(int fd)
- {
- int nfound, nfds;
- fd_set readfds;
- static struct timeval timeout = {0, 100};
-
- nfds = fd+1;
- FD_ZERO(&readfds);
- FD_SET(fd, &readfds);
- nfound = select(nfds,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout);
- if (nfound > 0 && FD_ISSET(fd, &readfds))
- return 1;
- return 0;
- }
-
-
- /*
- || count_items
- ||
- || Return number of columns in the input buf, where a column is
- || defined to be a contiguous group of non-whitespace characters.
- ||
- */
- #define NONWHITE 1
- #define ISWHITE 0
-
- int count_items(const char buf[])
- {
- char ch;
- int lastchar,i,items;
-
- /* blow off leading white-space */
- i=0;
- while(buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n') i++;
-
- if (buf[i] == '\0')
- return 0;
-
- items = 0;
- lastchar = NONWHITE;
- do
- {
- ch = buf[i];
- if(buf[i]== ' '||buf[i]== '\t'||buf[i]== '\n' ||buf[i] == '\0')
- { /* char is white-space */
- if(lastchar == NONWHITE)
- items++;
- lastchar = ISWHITE;
- }
- else
- { /* current char isn't a white space */
- lastchar = NONWHITE;
- }
- i++;
- } while(ch != '\n' && ch != '\0');
- return items;
- }
-
- #undef ISWHITE
- #undef NONWHITE
-
- /*
- || get_dir()
- ||
- || return directory strings extracted from a colon-separated
- || $PATH-style string. Mal-formed paths (eg "/bin::/usr/bin:")
- || pose no problem and are handled as expected.
- ||
- || Example use:
- ||
- || int i=0; (init i=0 and don't change it)
- || char *dir;
- || char *pathvar = "/bin:/usr/bin:/usr/etc:/usr/local/bin";
- || :
- || while ((dir=get_dir(pathvar, &i)) != NULL)
- || {
- || : (use dir in this iteration)
- || }
- ||
- */
- char *get_dir(const char *sp, int *pi)
- {
- static char dir[FILENAME_MAX];
- int i,j;
-
- /* *pi is the first char to test in this invocation */
-
- if (sp[*pi] == '\0')
- {
- *pi = 0;
- return NULL;
- }
-
- for (i = *pi, j=0 ; ; i++)
- {
- switch (sp[i])
- {
- case ':':
- dir[j] = '\0';
- if (j != 0) /* "::" case, we cycle */
- {
- *pi = i+1; /* get on other side of : for next invocation */
- return dir;
- }
- break;
- case '\0':
- dir[j] = '\0';
- if (j != 0)
- {
- *pi = i;
- return dir;
- }
- else /* ":\0" case */
- {
- *pi = 0;
- return NULL;
- }
- /* NOTREACHED */
- break;
- default:
- dir[j] = sp[i]; /* accumulate result string */
- j++;
- break;
- }
- }
- }
-
-
- /*
- || can_run
- ||
- || Look for the program named in the prog input in the user's $PATH...
- || Return OK if an exec(prog) would work.
- || NOT_FOUND if prog is not in the $PATH or $PATH is not set.
- || NOT_EXEC if prog is found but isn't executable for the user
- || or isn't a regular file.
- */
- int can_run(const char *prog)
- {
- char *searchpath;
- int p, status;
- char *dir;
- char abspath[FILENAME_MAX];
- struct stat statbuf;
-
- if ((searchpath = getenv("PATH")) == NULL)
- return NOT_FOUND;
-
- p = 0;
- status = NOT_FOUND; /* assume not found */
- while ((dir = get_dir(searchpath, &p)) != NULL)
- {
- strcpy(abspath, dir);
- strcat(abspath, "/");
- strcat(abspath, prog);
- if (stat(abspath, &statbuf) != -1)
- {
- if (S_ISREG(statbuf.st_mode))
- {
- if (statbuf.st_mode & S_IXUSR ||statbuf.st_mode & S_IXGRP ||
- statbuf.st_mode & S_IXOTH)
- status = OK; /* ok */
- else
- status = NOT_EXEC; /* not executable */
- }
- else /* not a regular file */
- status = NOT_EXEC;
- break;
- }
- }
-
- return status;
- }
-
-
- /*
- || print_nonprint
- ||
- || Print a string, map its non-printing characters into a visible
- || representation:
- || HOW_OCTAL \nnn
- || HOW_HEX \xhh
- || HOW_ESCAPE \a \b \f \n \r \t \v \nnn
- || HOW_QUES ?
- */
- void print_nonprint(const char *buf, int how)
- {
- while (*buf != '\0')
- {
- if (isprint((int) *buf))
- fputc((int)(*buf),stdout);
- else
- switch (how)
- {
- case HOW_OCTAL:
- printf("\\%03o",(unsigned) *buf);
- break;
- case HOW_HEX:
- printf("\\x%02x",(unsigned) *buf);
- break;
- case HOW_ESCAPE:
- switch (*buf)
- {
- case '\a': /* alert (bell) */
- fputs("\\a",stdout);
- break;
- case '\b': /* backspace */
- fputs("\\b",stdout);
- break;
- case '\f': /* formfeed */
- fputs("\\f",stdout);
- break;
- case '\n': /* newline */
- fputs("\\n",stdout);
- break;
- case '\r': /* carriage return */
- fputs("\\r",stdout);
- break;
- case '\t': /* horiz tab */
- fputs("\\t",stdout);
- break;
- case '\v': /* vertical tab */
- fputs("\\v",stdout);
- break;
- default:
- printf("\\%03o",(unsigned) *buf);
- /*fputc('?',stdout);*/
- break;
- }
- break;
- case HOW_QUES:
- default:
- fputc('?',stdout);
- break;
- }
- buf++;
- }
- printf("\n");
- }
-